package com.jy.wms.service;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.jy.wms.common.UUIDUtils;
import com.jy.wms.common.annotions.BaseServiceDescribe;
import com.jy.wms.common.base.BaseService;
import com.jy.wms.common.base.ServiceException;
import com.jy.wms.common.base.SucessException;
import com.jy.wms.dao.WmsLibrarypositiongroupMapper;
import com.jy.wms.pojo.WmsLibraryPosition;
import com.jy.wms.pojo.WmsLibrarypositiongroup;
import com.jy.wms.pojo.WmsLibrarypositiongroupDetailed;
import com.jy.wms.pojo.WmsOwner;
import net.sf.json.JSONObject;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
* @ClassName: WmsLibrarypositiongroupService.java
* @Description: TODO(这里用一句话描述这个类的作用)
* @Author syh
* @Date Thu Sep 28 10:59:21 CST 2017
*/

@Service
public class WmsLibrarypositiongroupService extends BaseService<WmsLibrarypositiongroupMapper,WmsLibrarypositiongroup> {

 /**
 * 添加库位
 * @param entity 实体对象
 * @return int
 */
  @BaseServiceDescribe(moduleName = "库位组管理", btnName = "新增库位组")
    public void baseInsert(WmsLibrarypositiongroup entity) throws Exception {
            if(entity.getList().size()>0) {
                entity.setKws(String.valueOf(entity.getList().size()));//库位组里的库位数是库位的数量
                Integer i = dao.baseInsert(entity);
                if (i > 0) {
                    WmsLibrarypositiongroupDetailed wmsLibrarypositiongroupDetailed=null;
                    for (WmsLibraryPosition wmsLibraryPosition : entity.getList()) {
                        wmsLibrarypositiongroupDetailed = new WmsLibrarypositiongroupDetailed();
                        wmsLibrarypositiongroupDetailed.setId(UUIDUtils.uuid());
                        wmsLibrarypositiongroupDetailed.setSskwzid(entity.getId());
                        wmsLibrarypositiongroupDetailed.setKwid(wmsLibraryPosition.getId());
                        wmsLibrarypositiongroupDetailed.setCkid(entity.getCkid());
                        Integer j = dao.insertDetailed(wmsLibrarypositiongroupDetailed);
                        if(j<=0){
                            throw new ServiceException("新增库位组明细失败");
                        }
                    }
                    throw new SucessException("新增库位组成功");
                }else {
                    throw new ServiceException("新增库位组失败");
                }
            }else {
                throw new ServiceException("库位组下无相应库位明细");
            }
    }
/**
* 插入一条记录（匹配有值的字段）
* @param entity 实体对象
* @param database 数据库名称
* @return int
*/
public Integer baseInsertSelective(WmsLibrarypositiongroup entity, String database ){
     return dao.baseInsertSelective( entity,database );
}

/**
* 删除（根据主键ID删除）
* @param id 主键
* @param database 数据库名称
* @return int
*/
public Integer baseDeleteByPrimaryKey(String id, String database ){
     return dao.baseDeleteByPrimaryKey( id,database );
}

/**
* 删除（根据ID 批量删除）
* @param idList 主键ID列表
* @return int
*/
public Integer baseDeleteBathPrimaryKeys(List<String> idList ){
     return dao.baseDeleteBathPrimaryKeys( idList );
}

/**
* 分页查询
* @param entity 实体
* @return Page<WmsLibrarypositiongroup>
*/
 @BaseServiceDescribe(moduleName = "库位组管理", btnName = "查询库位组")
    public Page<WmsLibrarypositiongroup> baseSelectPage(WmsLibrarypositiongroup entity) throws ServiceException {
        try {
            PageHelper.startPage(entity.getPage(), entity.getRows());
            return (Page<WmsLibrarypositiongroup>) dao.baseSelectPage(entity);
        } catch (Exception e) {
            throw new ServiceException("查询库位组失败", e);
        }

    }

    /**
     * 查看库位按钮
     * @param wmsLibraryPosition
     * @return
     * @throws ServiceException
     */
    @BaseServiceDescribe(moduleName = "库位组管理", btnName = "查看库位")
    public Page<WmsLibraryPosition> getPosition(WmsLibraryPosition wmsLibraryPosition) throws ServiceException {
        try {
            PageHelper.startPage(wmsLibraryPosition.getPage(), wmsLibraryPosition.getRows());
            return (Page<WmsLibraryPosition>) dao.getPosition(wmsLibraryPosition.getKwzid());
        } catch (Exception e) {
            throw new ServiceException("查看库位失败", e);
        }

    }

    /**
     * (添加里的)查询库位,查询未在库位组下的库位集合
     * @param wmsLibraryPosition
     * @return
     * @throws ServiceException
     */
    @BaseServiceDescribe(moduleName = "库位组管理", btnName = "查询库位")
    public Page<WmsLibraryPosition> seachKw(WmsLibraryPosition wmsLibraryPosition) throws ServiceException, Exception {
        try {
            //取库位组下的全部库位
            List<String> stringList = dao.seachKw3(wmsLibraryPosition.getKwzid());
            //库位组-库位
            //0收货-4暂存库位
            //1上架-0 存储库位
            //2集货- 5集货库位
            //3作业区- 0 存储库位, 1件拣货位,2箱拣货位 ,3箱/件拣货位
            String kwzlx=wmsLibraryPosition.getKwzlx().trim();
            List<String> list=new ArrayList<>();
            if("0".equals(kwzlx)){
                list.add("4");
            }else if("1".equals(kwzlx)){
                list.add("0");
            }else if("2".equals(kwzlx)){
                list.add("5");
            }else{
                list.add("0");
                list.add("1");
                list.add("2");
                list.add("3");
            }
            PageHelper.startPage(wmsLibraryPosition.getPage(), wmsLibraryPosition.getRows());
            if (stringList.size() > 0) {
                return (Page<WmsLibraryPosition>) dao.seachKw(wmsLibraryPosition, list, stringList);
            } else {
                return (Page<WmsLibraryPosition>) dao.seachKw4(wmsLibraryPosition, list);
            }
        } catch (Exception e) {
            //e.printStackTrace();
            throw new ServiceException("查询库位失败", e);
        }
    }

    /**
     * 修改时调用的查全部库位的(包含修改已有的库位和未在所有库位组的库位)
     * @param wmsLibraryPosition
     * @return
     * @throws ServiceException
     */
    @BaseServiceDescribe(moduleName = "库位组管理", btnName = "查询库位")
    public JSONObject seachKw2(WmsLibraryPosition wmsLibraryPosition) throws ServiceException {
        JSONObject json=new JSONObject();
        try {
            PageHelper.startPage(wmsLibraryPosition.getPage(), wmsLibraryPosition.getRows());
            //库位组-库位
            //0收货-4暂存库位
            //1上架-0 存储库位
            //2集货- 5集货库位
            //3作业区- 0 存储库位, 1件拣货位,2箱拣货位 ,3箱/件拣货位
            String kwzlx=wmsLibraryPosition.getKwzlx().trim();
            List<String> list=new ArrayList<>();
            if("0".equals(kwzlx)){
                list.add("4");
            }else if("1".equals(kwzlx)){
                list.add("0");
            }else if("2".equals(kwzlx)){
                list.add("5");
            }else{
                list.add("0");
                list.add("1");
                list.add("2");
                list.add("3");
            }
            Page<WmsLibraryPosition> wmsLibraryPositionPage=(Page<WmsLibraryPosition>) dao.seachKw2(wmsLibraryPosition,list);
            json.put("contentList",wmsLibraryPositionPage);
            json.put("totals",wmsLibraryPositionPage.getTotal());
            return json;
        } catch (Exception e) {
            throw new ServiceException("查询库位失败", e);
        }
    }

    /**
     * 用库位组id查 ids库位集合(修改时已有的库位)
     * @param wmsLibraryPosition
     * @return
     * @throws ServiceException
     */
    public Page<WmsLibraryPosition> getWmsLibrarypositionIds(WmsLibraryPosition wmsLibraryPosition) throws Exception {
        try {
            //用库位组的id,查库位的ids集合,现已有的
            PageHelper.startPage(wmsLibraryPosition.getPage(), wmsLibraryPosition.getRows());
            return (Page<WmsLibraryPosition>) dao.selectKWjh(wmsLibraryPosition.getKwzid());
        }catch (Exception e){
            throw new ServiceException("查询库位失败");
        }
    }



    /**
     * 查询用户所属全部货主
     * @param ckid
     * @return
     */
    public Page<WmsOwner> addSearch(String ckid, String hzmc ){
        PageHelper.startPage(0,20);
        return (Page<WmsOwner>)dao.addSearch( ckid,hzmc );
    }
    //添加去重
    public WmsLibrarypositiongroup searchAdd(String mc, String ckid, String sshzid){
        return dao.searchAdd(mc,ckid,sshzid);
    }

/**
* 编辑查询(根据库位组的id),以及查询现有库位集合数据
* @param id 主键ID
* @return WmsLibrarypositiongroup selectBySfyx
*/
@BaseServiceDescribe(moduleName = "库位组管理", btnName = "查看库位组")
public WmsLibrarypositiongroup baseSelectByPrimaryKey(String id) throws ServiceException {
        WmsLibrarypositiongroup wmsLibrarypositiongroup= dao.baseSelectByPrimaryKey(id);
        if(wmsLibrarypositiongroup!=null){
            return wmsLibrarypositiongroup;
        }else {
            throw new ServiceException("查看库位组失败");
        }
}
    /**
     * 查看库位组是否激活
     * @param id
     * @throws ServiceException
     */
    public void selectBySfyx( String id) throws ServiceException {
        WmsLibrarypositiongroup wmsLibrarypositiongroup= dao.selectBySfyx(id);
        if(("0").equals(wmsLibrarypositiongroup.getSfyx())){
            throw new ServiceException("请选择已激活的数据");
        }
    }

/**
* 查询（根据ID 批量查询）
* @param idList 主键ID列表
* @param database 数据库名称
* @return list
*/
public Integer baseSelectBathPrimaryKeys(List<WmsLibrarypositiongroup> idList, String database ){
     return dao.baseSelectBathPrimaryKeys( idList,database );
}

    /**
    *修改保存（匹配有值的字段）
    * @param entity 主键ID列表
    * @return int
    */

    @BaseServiceDescribe(moduleName = "库位组管理", btnName = "编辑库位组")
    public void baseUpdateByPrimaryKeySelective(WmsLibrarypositiongroup entity) throws Exception {
            Integer i = dao.baseUpdateByPrimaryKeySelective(entity);//编辑保存库位组信息
            if (i > 0) {
                    throw new SucessException("编辑库位组成功");
            } else {
                throw new ServiceException("编辑库位组失败");
            }

    }

    /**
     * 点击保存库位
     * @param entity
     * @throws Exception
     */
    @BaseServiceDescribe(moduleName = "库位组管理", btnName = "编辑库位")
    public void editPosition(WmsLibrarypositiongroup entity) throws Exception {
        if(entity.getList().size()>0){
            Integer cd=entity.getList().size();
            WmsLibrarypositiongroup entity2=dao.baseSelectByPrimaryKey(entity.getId());
            entity2.setKws(String.valueOf(Integer.valueOf(entity2.getKws())+cd));//库位组里的库位数是库位的数量
            dao.updateKws(entity2.getId(),entity2.getKws());//用前台传来的库位组id和库位数,改库位数
            //dao.deleteKW(entity.getId());//编辑保存前删除库位组明细
            WmsLibrarypositiongroupDetailed wmsLibrarypositiongroupDetailed=new WmsLibrarypositiongroupDetailed();
                for( WmsLibraryPosition wmsLibraryPosition:entity.getList()){//循环遍历库位和库位组信息,添加库位组明细信息
                    wmsLibrarypositiongroupDetailed=new WmsLibrarypositiongroupDetailed();
                    wmsLibrarypositiongroupDetailed.setId(UUIDUtils.uuid());
                    wmsLibrarypositiongroupDetailed.setSskwzid(entity.getId());
                    wmsLibrarypositiongroupDetailed.setKwid(wmsLibraryPosition.getId());
                    wmsLibrarypositiongroupDetailed.setCkid(entity.getCkid());
                    Integer h=dao.insertDetailed(wmsLibrarypositiongroupDetailed);//编辑保存明细
                    if(h<=0){
                        throw new ServiceException("编辑库位失败");
                    }
                }
            throw new SucessException("编辑库位成功");
        }else{
            throw new ServiceException("该库位组下无相应库位");
        }
    }
/**
*修改（根据主键ID修改）
* @param entity 主键ID列表
* @param database 数据库名称
* @return int
*/
public Integer baseUpdateByPrimaryKey(WmsLibrarypositiongroup entity, String database ){
     return dao.baseUpdateByPrimaryKey( entity,database );
}
//修改去重
    public WmsLibrarypositiongroup searchAdds(String mc, String id, String ckid, String sshzid){
        return dao.searchAdds(mc,id,ckid,sshzid);
    }
    /**
     * 是否激活
     * @param ids
     * @param sfyx
     * @throws ServiceException
     */
    @BaseServiceDescribe(moduleName = "库位组管理",btnName = "是否激活")
    public void sfyx(List<String> ids, String sfyx)throws Exception {
        Integer i=0;
        for(String id:ids){
            i=dao.sfyx(id,sfyx);
            if(i>0){
                throw new SucessException("激活操作成功");
            }else{
                throw new ServiceException("激活操作失败");
            }
        }
    }

    /**
     * 删除已有库位
     * @param wmsLibrarypositiongroup
     * @throws Exception
     */
    @BaseServiceDescribe(moduleName = "库位组编辑管理",btnName = "删除库位")
    public void deletePosition(WmsLibrarypositiongroup wmsLibrarypositiongroup)throws Exception {
        WmsLibrarypositiongroup wmsLibrarypositiongroup2=dao.baseSelectByPrimaryKey(wmsLibrarypositiongroup.getId());
        String kws=wmsLibrarypositiongroup2.getKws();
        String kws2= String.valueOf(wmsLibrarypositiongroup.getIds().size());
        if(kws.equals(kws2)){
            throw new ServiceException("库位不可全部删除");
        }
        for(String id : wmsLibrarypositiongroup.getIds()){
            Integer j=dao.deletePosition(id);
            if(j<=0){
                throw new ServiceException("删除已有库位失败");
            }
        }
        String kws3 = String.valueOf(Integer.valueOf(wmsLibrarypositiongroup2.getKws()) - wmsLibrarypositiongroup.getIds().size());
        Integer k = dao.updateKws(wmsLibrarypositiongroup.getId(), kws3);
        if (k > 0) {
            throw new SucessException("删除已有库位成功");
        } else {
            throw new ServiceException("删除已有库位失败");
        }
    }

}
